package com.siha_back.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.siha_back.config.OSSConfig;
import com.siha_back.entity.dto.PageBean;
import com.siha_back.entity.dto.Resume;
import com.siha_back.mapper.ResumeMapper;
import com.siha_back.service.ResumeService;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.UUID;



/**
 * (Resume)表服务实现类
 *
 * @author makejava
 * @since 2025-03-19 10:01:39
 */
@Service()
public class ResumeServiceImpl implements ResumeService {
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private OSSConfig ossConfig;
    private static final Logger log = Logger.getLogger(ResumeServiceImpl.class.getName());

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public String queryById(Integer id) {
        String endpoint=ossConfig.getEndpoint();
        String ossKey=ossConfig.getAccessKeyId();
        String ossSecretKey=ossConfig.getAccessKeySecret();
        String bucketName=ossConfig.getBucketName();
        String objectName="Resume/"+id+"/Resume.pdf";
        OSS ossClient = new OSSClient(endpoint, ossKey, ossSecretKey);
        try {
            // 获取文件
            OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, objectName));
            // 读取文件内容
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    return "已从个人中心获取到简历";
                }
            }
        } catch (OSSException | IOException e) {
            if("NoSuchKey".equals(e.getMessage())){
                return "简历不存在，请先上传";
            }
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
        return "请先上传简历";
    }

    /**
     * 分页查询
     *
     * @param resume 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
//    @Override
//    public Page<Resume> queryByPage(Resume resume, PageRequest pageRequest) {
//        long total = this.resumeDao.count(resume);
//        return new PageImpl<>(this.resumeDao.queryAllByLimit(resume, pageRequest), pageRequest, total);
//    }

    /**
     * 新增数据
     *
     * @param resume 实例对象
     * @return 实例对象
     */
    @Override
    public Resume insert(Resume resume) {
        this.resumeMapper.insert(resume);
        return resume;
    }

    /**
     * 修改数据
     *
     * @param resume 实例对象
     * @return 实例对象
     */
    /*@Override
    public Resume update(Resume resume) {
        this.resumeMapper.update(resume);
        return this.queryById(resume.getId());
    }*/

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        String endpoint=ossConfig.getEndpoint();
        String ossKey=ossConfig.getAccessKeyId();
        String ossSecretKey=ossConfig.getAccessKeySecret();
        String bucketName=ossConfig.getBucketName();
        //查询文件是否存在
        String result=this.queryById(id);
        String objectName="Resume/"+id+"/testPDF.pdf";
        OSS ossClient = new OSSClient(endpoint, ossKey, ossSecretKey);
        ossClient.deleteObject(new GetObjectRequest(bucketName, objectName));
        return true;  //文件删除成功
    }

    @Override
    public List<Resume> list() {
     //   Map<String,Object> map = utils.ThreadLocalUtil.get();
       // Integer userId = (Integer)map.get("id");
        return resumeMapper.list();
    }
    @Override
    public PageBean<Resume> list1(Integer pageNum, Integer pageSize, String resumeName, String resumeType) {
        PageBean<Resume> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Resume> list= resumeMapper.list1(resumeName,resumeType);
        System.out.println(list);
        Page<Resume> pageInfo=(Page<Resume>) list;
        pb.setTotal(pageInfo.getTotal());
        pb.setItems(pageInfo.getResult());
        return pb;
    }


}
